package crmdna.api.endpoint;

import java.util.List;
import java.util.Map;

import javax.inject.Named;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.Nullable;
import com.google.appengine.api.users.User;

import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;
import crmdna.gspreadsheet.GSpreadSheet;
import crmdna.participant.Participant;

@Api(name = "participant")
public class ParticipantApi {

	public APIResponse uploadFromPublishedGoogleSpreadSheet(
			@Nullable @Named("client") String client,
			@Named("spreadSheetKey") String gsKey,
			@Named("programId") long programId,
			@Nullable @Named("maxLinesDefault2500") Integer maxLines,
			@Nullable @Named("showStackTraceIfErrorDefaultFalse") Boolean showStackTrace,
			User user) {

		try {

			if (null == client)
				client = "isha";

			List<Map<String, String>> listOfMap = GSpreadSheet
					.getPublishedSpreasheetAsListOfMap(gsKey, maxLines);

			Participant.uploadFromMap(client, listOfMap, programId,
					Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, "Success");

		} catch (Exception ex) {
			if (showStackTrace == null)
				showStackTrace = false;
			return new APIResponse(ex, showStackTrace);
		}
	}

	public APIResponse deleteAllParticipants(
			@Nullable @Named("client") String client,
			@Named("programId") long programId, 
			@Nullable @Named("showStackTraceIfErrorDefaultFalse") Boolean showStackTrace,
			User user) {

		try {

			if (null == client)
				client = "isha";

			Participant.deleteAllParticipants(client, programId,
					Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, "Success");

		} catch (Exception ex) {
			if (showStackTrace == null)
				showStackTrace = false;
			
			return new APIResponse(ex, showStackTrace);
		}
	}
	
	public APIResponse emailCSV(
			@Nullable @Named("client") String client,
			@Named("programId") long programId, 
			@Nullable @Named("showStackTraceIfErrorDefaultFalse") Boolean showStackTrace,
			User user) {

		try {

			if (null == client)
				client = "isha";
			
			Participant.emailCSV(client, programId, Utils.getLoginEmail(user));		

			return new APIResponse(Status.SUCCESS, "Email sent");

		} catch (Exception ex) {
			if (showStackTrace == null)
				showStackTrace = false;
			
			return new APIResponse(ex, showStackTrace);
		}
	}
}
